<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>webchannelbase_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_net_webchannel_webchannelbase_test.js.html">webchannelbase_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.labs.net.webChannel.WebChannelBase.
<a name="line17"></a> * @suppress {accessControls} Private methods are accessed for test purposes.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.labs.net.webChannel.webChannelBaseTest&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.functions&#39;);
<a name="line28"></a>goog.require(&#39;goog.json&#39;);
<a name="line29"></a>goog.require(&#39;goog.labs.net.webChannel.ChannelRequest&#39;);
<a name="line30"></a>goog.require(&#39;goog.labs.net.webChannel.ForwardChannelRequestPool&#39;);
<a name="line31"></a>goog.require(&#39;goog.labs.net.webChannel.WebChannelBase&#39;);
<a name="line32"></a>goog.require(&#39;goog.labs.net.webChannel.WebChannelBaseTransport&#39;);
<a name="line33"></a>goog.require(&#39;goog.labs.net.webChannel.WebChannelDebug&#39;);
<a name="line34"></a>goog.require(&#39;goog.labs.net.webChannel.Wire&#39;);
<a name="line35"></a>goog.require(&#39;goog.labs.net.webChannel.netUtils&#39;);
<a name="line36"></a>goog.require(&#39;goog.labs.net.webChannel.requestStats&#39;);
<a name="line37"></a>goog.require(&#39;goog.labs.net.webChannel.requestStats.Stat&#39;);
<a name="line38"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line39"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line40"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line41"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line42"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line43"></a>
<a name="line44"></a>goog.setTestOnly(&#39;goog.labs.net.webChannel.webChannelBaseTest&#39;);
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Delay between a network failure and the next network request.
<a name="line49"></a> */
<a name="line50"></a>var RETRY_TIME = 1000;
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * A really long time - used to make sure no more timeouts will fire.
<a name="line55"></a> */
<a name="line56"></a>var ALL_DAY_MS = 1000 * 60 * 60 * 24;
<a name="line57"></a>
<a name="line58"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line59"></a>
<a name="line60"></a>var channel;
<a name="line61"></a>var deliveredMaps;
<a name="line62"></a>var handler;
<a name="line63"></a>var mockClock;
<a name="line64"></a>var gotError;
<a name="line65"></a>var numStatEvents;
<a name="line66"></a>var lastStatEvent;
<a name="line67"></a>var numTimingEvents;
<a name="line68"></a>var lastPostSize;
<a name="line69"></a>var lastPostRtt;
<a name="line70"></a>var lastPostRetryCount;
<a name="line71"></a>
<a name="line72"></a>// Set to true to see the channel debug output in the browser window.
<a name="line73"></a>var debug = false;
<a name="line74"></a>// Debug message to print out when debug is true.
<a name="line75"></a>var debugMessage = &#39;&#39;;
<a name="line76"></a>
<a name="line77"></a>function debugToWindow(message) {
<a name="line78"></a>  if (debug) {
<a name="line79"></a>    debugMessage += message + &#39;&lt;br&gt;&#39;;
<a name="line80"></a>    goog.dom.getElement(&#39;debug&#39;).innerHTML = debugMessage;
<a name="line81"></a>  }
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Stubs goog.labs.net.webChannel.netUtils to always time out. It maintains the
<a name="line87"></a> * contract given by goog.labs.net.webChannel.netUtils.testNetwork, but always
<a name="line88"></a> * times out (calling callback(false)).
<a name="line89"></a> *
<a name="line90"></a> * stubNetUtils should be called in tests that require it before
<a name="line91"></a> * a call to testNetwork happens. It is reset at tearDown.
<a name="line92"></a> */
<a name="line93"></a>function stubNetUtils() {
<a name="line94"></a>  stubs.set(goog.labs.net.webChannel.netUtils, &#39;testLoadImage&#39;,
<a name="line95"></a>      function(url, timeout, callback) {
<a name="line96"></a>        goog.Timer.callOnce(goog.partial(callback, false), timeout);
<a name="line97"></a>      });
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Stubs goog.labs.net.webChannel.ForwardChannelRequestPool.isSpdyEnabled_
<a name="line103"></a> * to manage the max pool size for the forward channel.
<a name="line104"></a> *
<a name="line105"></a> * @param {boolean} spdyEnabled Whether SPDY is enabled for the test.
<a name="line106"></a> */
<a name="line107"></a>function stubSpdyCheck(spdyEnabled) {
<a name="line108"></a>  stubs.set(goog.labs.net.webChannel.ForwardChannelRequestPool,
<a name="line109"></a>      &#39;isSpdyEnabled_&#39;,
<a name="line110"></a>      function() {
<a name="line111"></a>        return spdyEnabled;
<a name="line112"></a>      });
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Mock ChannelRequest.
<a name="line119"></a> * @constructor
<a name="line120"></a> * @struct
<a name="line121"></a> * @final
<a name="line122"></a> */
<a name="line123"></a>var MockChannelRequest = function(channel, channelDebug, opt_sessionId,
<a name="line124"></a>    opt_requestId, opt_retryId) {
<a name="line125"></a>  this.channel_ = channel;
<a name="line126"></a>  this.channelDebug_ = channelDebug;
<a name="line127"></a>  this.sessionId_ = opt_sessionId;
<a name="line128"></a>  this.requestId_ = opt_requestId;
<a name="line129"></a>  this.successful_ = true;
<a name="line130"></a>  this.lastError_ = null;
<a name="line131"></a>  this.lastStatusCode_ = 200;
<a name="line132"></a>
<a name="line133"></a>  // For debugging, keep track of whether this is a back or forward channel.
<a name="line134"></a>  this.isBack = !!(opt_requestId == &#39;rpc&#39;);
<a name="line135"></a>  this.isForward = !this.isBack;
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>MockChannelRequest.prototype.postData_ = null;
<a name="line139"></a>
<a name="line140"></a>MockChannelRequest.prototype.requestStartTime_ = null;
<a name="line141"></a>
<a name="line142"></a>MockChannelRequest.prototype.setExtraHeaders = function(extraHeaders) {};
<a name="line143"></a>
<a name="line144"></a>MockChannelRequest.prototype.setTimeout = function(timeout) {};
<a name="line145"></a>
<a name="line146"></a>MockChannelRequest.prototype.setReadyStateChangeThrottle =
<a name="line147"></a>    function(throttle) {};
<a name="line148"></a>
<a name="line149"></a>MockChannelRequest.prototype.xmlHttpPost = function(uri, postData,
<a name="line150"></a>    decodeChunks) {
<a name="line151"></a>  this.channelDebug_.debug(&#39;---&gt; POST: &#39; + uri + &#39;, &#39; + postData + &#39;, &#39; +
<a name="line152"></a>      decodeChunks);
<a name="line153"></a>  this.postData_ = postData;
<a name="line154"></a>  this.requestStartTime_ = goog.now();
<a name="line155"></a>};
<a name="line156"></a>
<a name="line157"></a>MockChannelRequest.prototype.xmlHttpGet = function(uri, decodeChunks,
<a name="line158"></a>    opt_noClose) {
<a name="line159"></a>  this.channelDebug_.debug(&#39;&lt;--- GET: &#39; + uri + &#39;, &#39; + decodeChunks + &#39;, &#39; +
<a name="line160"></a>      opt_noClose);
<a name="line161"></a>  this.requestStartTime_ = goog.now();
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>MockChannelRequest.prototype.tridentGet = function(uri, usingSecondaryDomain) {
<a name="line165"></a>  this.channelDebug_.debug(&#39;&lt;---GET (T): &#39; + uri);
<a name="line166"></a>  this.requestStartTime_ = goog.now();
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>MockChannelRequest.prototype.sendUsingImgTag = function(uri) {
<a name="line170"></a>  this.requestStartTime_ = goog.now();
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>MockChannelRequest.prototype.cancel = function() {
<a name="line174"></a>  this.successful_ = false;
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>MockChannelRequest.prototype.getSuccess = function() {
<a name="line178"></a>  return this.successful_;
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>MockChannelRequest.prototype.getLastError = function() {
<a name="line182"></a>  return this.lastError_;
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>MockChannelRequest.prototype.getLastStatusCode = function() {
<a name="line186"></a>  return this.lastStatusCode_;
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>MockChannelRequest.prototype.getSessionId = function() {
<a name="line190"></a>  return this.sessionId_;
<a name="line191"></a>};
<a name="line192"></a>
<a name="line193"></a>MockChannelRequest.prototype.getRequestId = function() {
<a name="line194"></a>  return this.requestId_;
<a name="line195"></a>};
<a name="line196"></a>
<a name="line197"></a>MockChannelRequest.prototype.getPostData = function() {
<a name="line198"></a>  return this.postData_;
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>MockChannelRequest.prototype.getRequestStartTime = function() {
<a name="line202"></a>  return this.requestStartTime_;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * @suppress {invalidCasts} The cast from MockChannelRequest to
<a name="line208"></a> * ChannelRequest is invalid and will not compile.
<a name="line209"></a> */
<a name="line210"></a>function setUpPage() {
<a name="line211"></a>  // Use our MockChannelRequests instead of the real ones.
<a name="line212"></a>  goog.labs.net.webChannel.ChannelRequest.createChannelRequest = function(
<a name="line213"></a>      channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) {
<a name="line214"></a>    return /** @type {!goog.labs.net.webChannel.ChannelRequest} */ (
<a name="line215"></a>        new MockChannelRequest(channel, channelDebug, opt_sessionId,
<a name="line216"></a>            opt_requestId, opt_retryId));
<a name="line217"></a>  };
<a name="line218"></a>
<a name="line219"></a>  // Mock out the stat notification code.
<a name="line220"></a>  goog.labs.net.webChannel.requestStats.notifyStatEvent = function(
<a name="line221"></a>      stat) {
<a name="line222"></a>    numStatEvents++;
<a name="line223"></a>    lastStatEvent = stat;
<a name="line224"></a>  };
<a name="line225"></a>
<a name="line226"></a>  goog.labs.net.webChannel.requestStats.notifyTimingEvent = function(
<a name="line227"></a>      size, rtt, retries) {
<a name="line228"></a>    numTimingEvents++;
<a name="line229"></a>    lastPostSize = size;
<a name="line230"></a>    lastPostRtt = rtt;
<a name="line231"></a>    lastPostRetryCount = retries;
<a name="line232"></a>  };
<a name="line233"></a>}
<a name="line234"></a>
<a name="line235"></a>function setUp() {
<a name="line236"></a>  numTimingEvents = 0;
<a name="line237"></a>  lastPostSize = null;
<a name="line238"></a>  lastPostRtt = null;
<a name="line239"></a>  lastPostRetryCount = null;
<a name="line240"></a>
<a name="line241"></a>  mockClock = new goog.testing.MockClock(true);
<a name="line242"></a>  channel = new goog.labs.net.webChannel.WebChannelBase(&#39;1&#39;);
<a name="line243"></a>  gotError = false;
<a name="line244"></a>
<a name="line245"></a>  handler = new goog.labs.net.webChannel.WebChannelBase.Handler();
<a name="line246"></a>  handler.channelOpened = function() {};
<a name="line247"></a>  handler.channelError = function(channel, error) {
<a name="line248"></a>    gotError = true;
<a name="line249"></a>  };
<a name="line250"></a>  handler.channelSuccess = function(channel, maps) {
<a name="line251"></a>    deliveredMaps = goog.array.clone(maps);
<a name="line252"></a>  };
<a name="line253"></a>
<a name="line254"></a>  /**
<a name="line255"></a>   * @suppress {checkTypes} The callback function type declaration is skipped.
<a name="line256"></a>   */
<a name="line257"></a>  handler.channelClosed = function(
<a name="line258"></a>      channel, opt_pendingMaps, opt_undeliveredMaps) {
<a name="line259"></a>    // Mock out the handler, and let it set a formatted user readable string
<a name="line260"></a>    // of the undelivered maps which we can use when verifying our assertions.
<a name="line261"></a>    if (opt_pendingMaps) {
<a name="line262"></a>      handler.pendingMapsString = formatArrayOfMaps(opt_pendingMaps);
<a name="line263"></a>    }
<a name="line264"></a>    if (opt_undeliveredMaps) {
<a name="line265"></a>      handler.undeliveredMapsString = formatArrayOfMaps(opt_undeliveredMaps);
<a name="line266"></a>    }
<a name="line267"></a>  };
<a name="line268"></a>  handler.channelHandleMultipleArrays = function() {};
<a name="line269"></a>  handler.channelHandleArray = function() {};
<a name="line270"></a>
<a name="line271"></a>  channel.setHandler(handler);
<a name="line272"></a>
<a name="line273"></a>  // Provide a predictable retry time for testing.
<a name="line274"></a>  channel.getRetryTime_ = function(retryCount) {
<a name="line275"></a>    return RETRY_TIME;
<a name="line276"></a>  };
<a name="line277"></a>
<a name="line278"></a>  var channelDebug = new goog.labs.net.webChannel.WebChannelDebug();
<a name="line279"></a>  channelDebug.debug = function(message) {
<a name="line280"></a>    debugToWindow(message);
<a name="line281"></a>  };
<a name="line282"></a>  channel.setChannelDebug(channelDebug);
<a name="line283"></a>
<a name="line284"></a>  numStatEvents = 0;
<a name="line285"></a>  lastStatEvent = null;
<a name="line286"></a>}
<a name="line287"></a>
<a name="line288"></a>
<a name="line289"></a>function tearDown() {
<a name="line290"></a>  mockClock.dispose();
<a name="line291"></a>  stubs.reset();
<a name="line292"></a>  debugToWindow(&#39;&lt;hr&gt;&#39;);
<a name="line293"></a>}
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>function getSingleForwardRequest() {
<a name="line297"></a>  var pool = channel.forwardChannelRequestPool_;
<a name="line298"></a>  if (!pool.hasPendingRequest()) {
<a name="line299"></a>    return null;
<a name="line300"></a>  }
<a name="line301"></a>  return pool.request_ || pool.requestPool_.getValues()[0];
<a name="line302"></a>}
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * Helper function to return a formatted string representing an array of maps.
<a name="line307"></a> */
<a name="line308"></a>function formatArrayOfMaps(arrayOfMaps) {
<a name="line309"></a>  var result = [];
<a name="line310"></a>  for (var i = 0; i &lt; arrayOfMaps.length; i++) {
<a name="line311"></a>    var map = arrayOfMaps[i];
<a name="line312"></a>    var keys = map.map.getKeys();
<a name="line313"></a>    for (var j = 0; j &lt; keys.length; j++) {
<a name="line314"></a>      var tmp = keys[j] + &#39;:&#39; + map.map.get(keys[j]) + (map.context ?
<a name="line315"></a>          &#39;:&#39; + map.context : &#39;&#39;);
<a name="line316"></a>      result.push(tmp);
<a name="line317"></a>    }
<a name="line318"></a>  }
<a name="line319"></a>  return result.join(&#39;, &#39;);
<a name="line320"></a>}
<a name="line321"></a>
<a name="line322"></a>
<a name="line323"></a>function testFormatArrayOfMaps() {
<a name="line324"></a>  // This function is used in a non-trivial test, so let&#39;s verify that it works.
<a name="line325"></a>  var map1 = new goog.structs.Map();
<a name="line326"></a>  map1.set(&#39;k1&#39;, &#39;v1&#39;);
<a name="line327"></a>  map1.set(&#39;k2&#39;, &#39;v2&#39;);
<a name="line328"></a>  var map2 = new goog.structs.Map();
<a name="line329"></a>  map2.set(&#39;k3&#39;, &#39;v3&#39;);
<a name="line330"></a>  var map3 = new goog.structs.Map();
<a name="line331"></a>  map3.set(&#39;k4&#39;, &#39;v4&#39;);
<a name="line332"></a>  map3.set(&#39;k5&#39;, &#39;v5&#39;);
<a name="line333"></a>  map3.set(&#39;k6&#39;, &#39;v6&#39;);
<a name="line334"></a>
<a name="line335"></a>  // One map.
<a name="line336"></a>  var a = [];
<a name="line337"></a>  a.push(new goog.labs.net.webChannel.Wire.QueuedMap(0, map1));
<a name="line338"></a>  assertEquals(&#39;k1:v1, k2:v2&#39;,
<a name="line339"></a>      formatArrayOfMaps(a));
<a name="line340"></a>
<a name="line341"></a>  // Many maps.
<a name="line342"></a>  var b = [];
<a name="line343"></a>  b.push(new goog.labs.net.webChannel.Wire.QueuedMap(0, map1));
<a name="line344"></a>  b.push(new goog.labs.net.webChannel.Wire.QueuedMap(0, map2));
<a name="line345"></a>  b.push(new goog.labs.net.webChannel.Wire.QueuedMap(0, map3));
<a name="line346"></a>  assertEquals(&#39;k1:v1, k2:v2, k3:v3, k4:v4, k5:v5, k6:v6&#39;,
<a name="line347"></a>      formatArrayOfMaps(b));
<a name="line348"></a>
<a name="line349"></a>  // One map with a context.
<a name="line350"></a>  var c = [];
<a name="line351"></a>  c.push(new goog.labs.net.webChannel.Wire.QueuedMap(
<a name="line352"></a>      0, map1, new String(&#39;c1&#39;)));
<a name="line353"></a>  assertEquals(&#39;k1:v1:c1, k2:v2:c1&#39;,
<a name="line354"></a>      formatArrayOfMaps(c));
<a name="line355"></a>}
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/**
<a name="line359"></a> * @param {number=} opt_serverVersion
<a name="line360"></a> * @param {string=} opt_hostPrefix
<a name="line361"></a> * @param {string=} opt_uriPrefix
<a name="line362"></a> * @param {boolean=} opt_spdyEnabled
<a name="line363"></a> */
<a name="line364"></a>function connectForwardChannel(
<a name="line365"></a>    opt_serverVersion, opt_hostPrefix, opt_uriPrefix, opt_spdyEnabled) {
<a name="line366"></a>  stubSpdyCheck(!!opt_spdyEnabled);
<a name="line367"></a>  var uriPrefix = opt_uriPrefix || &#39;&#39;;
<a name="line368"></a>  channel.connect(uriPrefix + &#39;/test&#39;, uriPrefix + &#39;/bind&#39;, null);
<a name="line369"></a>  mockClock.tick(0);
<a name="line370"></a>  completeTestConnection();
<a name="line371"></a>  completeForwardChannel(opt_serverVersion, opt_hostPrefix);
<a name="line372"></a>}
<a name="line373"></a>
<a name="line374"></a>
<a name="line375"></a>/**
<a name="line376"></a> * @param {number=} opt_serverVersion
<a name="line377"></a> * @param {string=} opt_hostPrefix
<a name="line378"></a> * @param {string=} opt_uriPrefix
<a name="line379"></a> * @param {boolean=} opt_spdyEnabled
<a name="line380"></a> */
<a name="line381"></a>function connect(opt_serverVersion, opt_hostPrefix, opt_uriPrefix,
<a name="line382"></a>    opt_spdyEnabled) {
<a name="line383"></a>  connectForwardChannel(opt_serverVersion, opt_hostPrefix, opt_uriPrefix,
<a name="line384"></a>      opt_spdyEnabled);
<a name="line385"></a>  completeBackChannel();
<a name="line386"></a>}
<a name="line387"></a>
<a name="line388"></a>
<a name="line389"></a>function disconnect() {
<a name="line390"></a>  channel.disconnect();
<a name="line391"></a>  mockClock.tick(0);
<a name="line392"></a>}
<a name="line393"></a>
<a name="line394"></a>
<a name="line395"></a>function completeTestConnection() {
<a name="line396"></a>  completeForwardTestConnection();
<a name="line397"></a>  completeBackTestConnection();
<a name="line398"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.OPENING,
<a name="line399"></a>      channel.getState());
<a name="line400"></a>}
<a name="line401"></a>
<a name="line402"></a>
<a name="line403"></a>function completeForwardTestConnection() {
<a name="line404"></a>  channel.connectionTest_.onRequestData(
<a name="line405"></a>      channel.connectionTest_,
<a name="line406"></a>      &#39;[&quot;b&quot;]&#39;);
<a name="line407"></a>  channel.connectionTest_.onRequestComplete(
<a name="line408"></a>      channel.connectionTest_);
<a name="line409"></a>  mockClock.tick(0);
<a name="line410"></a>}
<a name="line411"></a>
<a name="line412"></a>
<a name="line413"></a>function completeBackTestConnection() {
<a name="line414"></a>  channel.connectionTest_.onRequestData(
<a name="line415"></a>      channel.connectionTest_,
<a name="line416"></a>      &#39;11111&#39;);
<a name="line417"></a>  mockClock.tick(0);
<a name="line418"></a>}
<a name="line419"></a>
<a name="line420"></a>
<a name="line421"></a>/**
<a name="line422"></a> * @param {number=} opt_serverVersion
<a name="line423"></a> * @param {string=} opt_hostPrefix
<a name="line424"></a> */
<a name="line425"></a>function completeForwardChannel(opt_serverVersion, opt_hostPrefix) {
<a name="line426"></a>  var responseData = &#39;[[0,[&quot;c&quot;,&quot;1234567890ABCDEF&quot;,&#39; +
<a name="line427"></a>      (opt_hostPrefix ? &#39;&quot;&#39; + opt_hostPrefix + &#39;&quot;&#39; : &#39;null&#39;) +
<a name="line428"></a>      (opt_serverVersion ? &#39;,&#39; + opt_serverVersion : &#39;&#39;) +
<a name="line429"></a>      &#39;]]]&#39;;
<a name="line430"></a>  channel.onRequestData(
<a name="line431"></a>      getSingleForwardRequest(),
<a name="line432"></a>      responseData);
<a name="line433"></a>  channel.onRequestComplete(
<a name="line434"></a>      getSingleForwardRequest());
<a name="line435"></a>  mockClock.tick(0);
<a name="line436"></a>}
<a name="line437"></a>
<a name="line438"></a>
<a name="line439"></a>function completeBackChannel() {
<a name="line440"></a>  channel.onRequestData(
<a name="line441"></a>      channel.backChannelRequest_,
<a name="line442"></a>      &#39;[[1,[&quot;foo&quot;]]]&#39;);
<a name="line443"></a>  channel.onRequestComplete(
<a name="line444"></a>      channel.backChannelRequest_);
<a name="line445"></a>  mockClock.tick(0);
<a name="line446"></a>}
<a name="line447"></a>
<a name="line448"></a>
<a name="line449"></a>function responseDone() {
<a name="line450"></a>  channel.onRequestData(
<a name="line451"></a>      getSingleForwardRequest(),
<a name="line452"></a>      &#39;[1,0,0]&#39;);  // mock data
<a name="line453"></a>  channel.onRequestComplete(
<a name="line454"></a>      getSingleForwardRequest());
<a name="line455"></a>  mockClock.tick(0);
<a name="line456"></a>}
<a name="line457"></a>
<a name="line458"></a>
<a name="line459"></a>/**
<a name="line460"></a> *
<a name="line461"></a> * @param {number=} opt_lastArrayIdSentFromServer
<a name="line462"></a> * @param {number=} opt_outstandingDataSize
<a name="line463"></a> */
<a name="line464"></a>function responseNoBackchannel(
<a name="line465"></a>    opt_lastArrayIdSentFromServer, opt_outstandingDataSize) {
<a name="line466"></a>  var responseData = goog.json.serialize(
<a name="line467"></a>      [0, opt_lastArrayIdSentFromServer, opt_outstandingDataSize]);
<a name="line468"></a>  channel.onRequestData(
<a name="line469"></a>      getSingleForwardRequest(),
<a name="line470"></a>      responseData);
<a name="line471"></a>  channel.onRequestComplete(
<a name="line472"></a>      getSingleForwardRequest());
<a name="line473"></a>  mockClock.tick(0);
<a name="line474"></a>}
<a name="line475"></a>
<a name="line476"></a>function response(lastArrayIdSentFromServer, outstandingDataSize) {
<a name="line477"></a>  var responseData = goog.json.serialize(
<a name="line478"></a>      [1, lastArrayIdSentFromServer, outstandingDataSize]);
<a name="line479"></a>  channel.onRequestData(
<a name="line480"></a>      getSingleForwardRequest(),
<a name="line481"></a>      responseData
<a name="line482"></a>  );
<a name="line483"></a>  channel.onRequestComplete(
<a name="line484"></a>      getSingleForwardRequest());
<a name="line485"></a>  mockClock.tick(0);
<a name="line486"></a>}
<a name="line487"></a>
<a name="line488"></a>
<a name="line489"></a>function receive(data) {
<a name="line490"></a>  channel.onRequestData(
<a name="line491"></a>      channel.backChannelRequest_,
<a name="line492"></a>      &#39;[[1,&#39; + data + &#39;]]&#39;);
<a name="line493"></a>  channel.onRequestComplete(
<a name="line494"></a>      channel.backChannelRequest_);
<a name="line495"></a>  mockClock.tick(0);
<a name="line496"></a>}
<a name="line497"></a>
<a name="line498"></a>
<a name="line499"></a>function responseTimeout() {
<a name="line500"></a>  getSingleForwardRequest().lastError_ =
<a name="line501"></a>      goog.labs.net.webChannel.ChannelRequest.Error.TIMEOUT;
<a name="line502"></a>  getSingleForwardRequest().successful_ = false;
<a name="line503"></a>  channel.onRequestComplete(
<a name="line504"></a>      getSingleForwardRequest());
<a name="line505"></a>  mockClock.tick(0);
<a name="line506"></a>}
<a name="line507"></a>
<a name="line508"></a>
<a name="line509"></a>/**
<a name="line510"></a> * @param {number=} opt_statusCode
<a name="line511"></a> */
<a name="line512"></a>function responseRequestFailed(opt_statusCode) {
<a name="line513"></a>  getSingleForwardRequest().lastError_ =
<a name="line514"></a>      goog.labs.net.webChannel.ChannelRequest.Error.STATUS;
<a name="line515"></a>  getSingleForwardRequest().lastStatusCode_ =
<a name="line516"></a>      opt_statusCode || 503;
<a name="line517"></a>  getSingleForwardRequest().successful_ = false;
<a name="line518"></a>  channel.onRequestComplete(
<a name="line519"></a>      getSingleForwardRequest());
<a name="line520"></a>  mockClock.tick(0);
<a name="line521"></a>}
<a name="line522"></a>
<a name="line523"></a>
<a name="line524"></a>function responseUnknownSessionId() {
<a name="line525"></a>  getSingleForwardRequest().lastError_ =
<a name="line526"></a>      goog.labs.net.webChannel.ChannelRequest.Error.UNKNOWN_SESSION_ID;
<a name="line527"></a>  getSingleForwardRequest().successful_ = false;
<a name="line528"></a>  channel.onRequestComplete(
<a name="line529"></a>      getSingleForwardRequest());
<a name="line530"></a>  mockClock.tick(0);
<a name="line531"></a>}
<a name="line532"></a>
<a name="line533"></a>
<a name="line534"></a>function responseActiveXBlocked() {
<a name="line535"></a>  channel.backChannelRequest_.lastError_ =
<a name="line536"></a>      goog.labs.net.webChannel.ChannelRequest.Error.ACTIVE_X_BLOCKED;
<a name="line537"></a>  channel.backChannelRequest_.successful_ = false;
<a name="line538"></a>  channel.onRequestComplete(
<a name="line539"></a>      channel.backChannelRequest_);
<a name="line540"></a>  mockClock.tick(0);
<a name="line541"></a>}
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>/**
<a name="line545"></a> * @param {string} key
<a name="line546"></a> * @param {string} value
<a name="line547"></a> * @param {string=} opt_context
<a name="line548"></a> */
<a name="line549"></a>function sendMap(key, value, opt_context) {
<a name="line550"></a>  var map = new goog.structs.Map();
<a name="line551"></a>  map.set(key, value);
<a name="line552"></a>  channel.sendMap(map, opt_context);
<a name="line553"></a>  mockClock.tick(0);
<a name="line554"></a>}
<a name="line555"></a>
<a name="line556"></a>
<a name="line557"></a>function hasForwardChannel() {
<a name="line558"></a>  return !!getSingleForwardRequest();
<a name="line559"></a>}
<a name="line560"></a>
<a name="line561"></a>
<a name="line562"></a>function hasBackChannel() {
<a name="line563"></a>  return !!channel.backChannelRequest_;
<a name="line564"></a>}
<a name="line565"></a>
<a name="line566"></a>
<a name="line567"></a>function hasDeadBackChannelTimer() {
<a name="line568"></a>  return goog.isDefAndNotNull(channel.deadBackChannelTimerId_);
<a name="line569"></a>}
<a name="line570"></a>
<a name="line571"></a>
<a name="line572"></a>function assertHasForwardChannel() {
<a name="line573"></a>  assertTrue(&#39;Forward channel missing.&#39;, hasForwardChannel());
<a name="line574"></a>}
<a name="line575"></a>
<a name="line576"></a>
<a name="line577"></a>function assertHasBackChannel() {
<a name="line578"></a>  assertTrue(&#39;Back channel missing.&#39;, hasBackChannel());
<a name="line579"></a>}
<a name="line580"></a>
<a name="line581"></a>
<a name="line582"></a>function testConnect() {
<a name="line583"></a>  connect();
<a name="line584"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.OPENED,
<a name="line585"></a>      channel.getState());
<a name="line586"></a>  // If the server specifies no version, the client assumes the latest version
<a name="line587"></a>  assertEquals(goog.labs.net.webChannel.Wire.LATEST_CHANNEL_VERSION,
<a name="line588"></a>               channel.channelVersion_);
<a name="line589"></a>  assertFalse(channel.isBuffered());
<a name="line590"></a>}
<a name="line591"></a>
<a name="line592"></a>function testConnect_backChannelEstablished() {
<a name="line593"></a>  connect();
<a name="line594"></a>  assertHasBackChannel();
<a name="line595"></a>}
<a name="line596"></a>
<a name="line597"></a>function testConnect_withServerHostPrefix() {
<a name="line598"></a>  connect(undefined, &#39;serverHostPrefix&#39;);
<a name="line599"></a>  assertEquals(&#39;serverHostPrefix&#39;, channel.hostPrefix_);
<a name="line600"></a>}
<a name="line601"></a>
<a name="line602"></a>function testConnect_withClientHostPrefix() {
<a name="line603"></a>  handler.correctHostPrefix = function(hostPrefix) {
<a name="line604"></a>    return &#39;clientHostPrefix&#39;;
<a name="line605"></a>  };
<a name="line606"></a>  connect();
<a name="line607"></a>  assertEquals(&#39;clientHostPrefix&#39;, channel.hostPrefix_);
<a name="line608"></a>}
<a name="line609"></a>
<a name="line610"></a>function testConnect_overrideServerHostPrefix() {
<a name="line611"></a>  handler.correctHostPrefix = function(hostPrefix) {
<a name="line612"></a>    return &#39;clientHostPrefix&#39;;
<a name="line613"></a>  };
<a name="line614"></a>  connect(undefined, &#39;serverHostPrefix&#39;);
<a name="line615"></a>  assertEquals(&#39;clientHostPrefix&#39;, channel.hostPrefix_);
<a name="line616"></a>}
<a name="line617"></a>
<a name="line618"></a>function testConnect_withServerVersion() {
<a name="line619"></a>  connect(8);
<a name="line620"></a>  assertEquals(8, channel.channelVersion_);
<a name="line621"></a>}
<a name="line622"></a>
<a name="line623"></a>function testConnect_notOkToMakeRequestForTest() {
<a name="line624"></a>  handler.okToMakeRequest = goog.functions.constant(
<a name="line625"></a>      goog.labs.net.webChannel.WebChannelBase.Error.NETWORK);
<a name="line626"></a>  channel.connect(&#39;/test&#39;, &#39;/bind&#39;, null);
<a name="line627"></a>  mockClock.tick(0);
<a name="line628"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.CLOSED,
<a name="line629"></a>               channel.getState());
<a name="line630"></a>}
<a name="line631"></a>
<a name="line632"></a>function testConnect_notOkToMakeRequestForBind() {
<a name="line633"></a>  channel.connect(&#39;/test&#39;, &#39;/bind&#39;, null);
<a name="line634"></a>  mockClock.tick(0);
<a name="line635"></a>  completeTestConnection();
<a name="line636"></a>  handler.okToMakeRequest = goog.functions.constant(
<a name="line637"></a>      goog.labs.net.webChannel.WebChannelBase.Error.NETWORK);
<a name="line638"></a>  completeForwardChannel();
<a name="line639"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.CLOSED,
<a name="line640"></a>               channel.getState());
<a name="line641"></a>}
<a name="line642"></a>
<a name="line643"></a>
<a name="line644"></a>function testSendMap() {
<a name="line645"></a>  connect();
<a name="line646"></a>  sendMapOnce();
<a name="line647"></a>}
<a name="line648"></a>
<a name="line649"></a>
<a name="line650"></a>function testSendMapWithSpdyEnabled() {
<a name="line651"></a>  connect(undefined, undefined, undefined, true);
<a name="line652"></a>  sendMapOnce();
<a name="line653"></a>}
<a name="line654"></a>
<a name="line655"></a>
<a name="line656"></a>function sendMapOnce() {
<a name="line657"></a>  assertEquals(1, numTimingEvents);
<a name="line658"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line659"></a>  responseDone();
<a name="line660"></a>  assertEquals(2, numTimingEvents);
<a name="line661"></a>  assertEquals(&#39;foo:bar&#39;, formatArrayOfMaps(deliveredMaps));
<a name="line662"></a>}
<a name="line663"></a>
<a name="line664"></a>
<a name="line665"></a>function testSendMap_twice() {
<a name="line666"></a>  connect();
<a name="line667"></a>  sendMapTwice();
<a name="line668"></a>}
<a name="line669"></a>
<a name="line670"></a>
<a name="line671"></a>function testSendMap_twiceWithSpdyEnabled() {
<a name="line672"></a>  connect(undefined, undefined, undefined, true);
<a name="line673"></a>  sendMapTwice();
<a name="line674"></a>}
<a name="line675"></a>
<a name="line676"></a>
<a name="line677"></a>function sendMapTwice() {
<a name="line678"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line679"></a>  responseDone();
<a name="line680"></a>  assertEquals(&#39;foo1:bar1&#39;, formatArrayOfMaps(deliveredMaps));
<a name="line681"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line682"></a>  responseDone();
<a name="line683"></a>  assertEquals(&#39;foo2:bar2&#39;, formatArrayOfMaps(deliveredMaps));
<a name="line684"></a>}
<a name="line685"></a>
<a name="line686"></a>
<a name="line687"></a>function testSendMap_andReceive() {
<a name="line688"></a>  connect();
<a name="line689"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line690"></a>  responseDone();
<a name="line691"></a>  receive(&#39;[&quot;the server reply&quot;]&#39;);
<a name="line692"></a>}
<a name="line693"></a>
<a name="line694"></a>
<a name="line695"></a>function testReceive() {
<a name="line696"></a>  connect();
<a name="line697"></a>  receive(&#39;[&quot;message from server&quot;]&#39;);
<a name="line698"></a>  assertHasBackChannel();
<a name="line699"></a>}
<a name="line700"></a>
<a name="line701"></a>
<a name="line702"></a>function testReceive_twice() {
<a name="line703"></a>  connect();
<a name="line704"></a>  receive(&#39;[&quot;message one from server&quot;]&#39;);
<a name="line705"></a>  receive(&#39;[&quot;message two from server&quot;]&#39;);
<a name="line706"></a>  assertHasBackChannel();
<a name="line707"></a>}
<a name="line708"></a>
<a name="line709"></a>
<a name="line710"></a>function testReceive_andSendMap() {
<a name="line711"></a>  connect();
<a name="line712"></a>  receive(&#39;[&quot;the server reply&quot;]&#39;);
<a name="line713"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line714"></a>  responseDone();
<a name="line715"></a>  assertHasBackChannel();
<a name="line716"></a>}
<a name="line717"></a>
<a name="line718"></a>
<a name="line719"></a>function testBackChannelRemainsEstablished_afterSingleSendMap() {
<a name="line720"></a>  connect();
<a name="line721"></a>
<a name="line722"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line723"></a>  responseDone();
<a name="line724"></a>  receive(&#39;[&quot;ack&quot;]&#39;);
<a name="line725"></a>
<a name="line726"></a>  assertHasBackChannel();
<a name="line727"></a>}
<a name="line728"></a>
<a name="line729"></a>
<a name="line730"></a>function testBackChannelRemainsEstablished_afterDoubleSendMap() {
<a name="line731"></a>  connect();
<a name="line732"></a>
<a name="line733"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line734"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line735"></a>  responseDone();
<a name="line736"></a>  receive(&#39;[&quot;ack&quot;]&#39;);
<a name="line737"></a>
<a name="line738"></a>  // This assertion would fail prior to CL 13302660.
<a name="line739"></a>  assertHasBackChannel();
<a name="line740"></a>}
<a name="line741"></a>
<a name="line742"></a>
<a name="line743"></a>function testTimingEvent() {
<a name="line744"></a>  connect();
<a name="line745"></a>  assertEquals(1, numTimingEvents);
<a name="line746"></a>  sendMap(&#39;&#39;, &#39;&#39;);
<a name="line747"></a>  assertEquals(1, numTimingEvents);
<a name="line748"></a>  mockClock.tick(20);
<a name="line749"></a>  var expSize = getSingleForwardRequest().getPostData().length;
<a name="line750"></a>  responseDone();
<a name="line751"></a>
<a name="line752"></a>  assertEquals(2, numTimingEvents);
<a name="line753"></a>  assertEquals(expSize, lastPostSize);
<a name="line754"></a>  assertEquals(20, lastPostRtt);
<a name="line755"></a>  assertEquals(0, lastPostRetryCount);
<a name="line756"></a>
<a name="line757"></a>  sendMap(&#39;abcdefg&#39;, &#39;123456&#39;);
<a name="line758"></a>  expSize = getSingleForwardRequest().getPostData().length;
<a name="line759"></a>  responseTimeout();
<a name="line760"></a>  assertEquals(2, numTimingEvents);
<a name="line761"></a>  mockClock.tick(RETRY_TIME + 1);
<a name="line762"></a>  responseDone();
<a name="line763"></a>  assertEquals(3, numTimingEvents);
<a name="line764"></a>  assertEquals(expSize, lastPostSize);
<a name="line765"></a>  assertEquals(1, lastPostRetryCount);
<a name="line766"></a>  assertEquals(1, lastPostRtt);
<a name="line767"></a>
<a name="line768"></a>}
<a name="line769"></a>
<a name="line770"></a>
<a name="line771"></a>/**
<a name="line772"></a> * Make sure that dropping the forward channel retry limit below the retry count
<a name="line773"></a> * reports an error, and prevents another request from firing.
<a name="line774"></a> */
<a name="line775"></a>function testSetFailFastWhileWaitingForRetry() {
<a name="line776"></a>  stubNetUtils();
<a name="line777"></a>
<a name="line778"></a>  connect();
<a name="line779"></a>  setFailFastWhileWaitingForRetry();
<a name="line780"></a>}
<a name="line781"></a>
<a name="line782"></a>
<a name="line783"></a>function testSetFailFastWhileWaitingForRetryWithSpdyEnabled() {
<a name="line784"></a>  stubNetUtils();
<a name="line785"></a>
<a name="line786"></a>  connect(undefined, undefined, undefined, true);
<a name="line787"></a>  setFailFastWhileWaitingForRetry();
<a name="line788"></a>}
<a name="line789"></a>
<a name="line790"></a>
<a name="line791"></a>function setFailFastWhileWaitingForRetry() {
<a name="line792"></a>  assertEquals(1, numTimingEvents);
<a name="line793"></a>
<a name="line794"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line795"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line796"></a>  assertNotNull(getSingleForwardRequest());
<a name="line797"></a>  assertEquals(0, channel.forwardChannelRetryCount_);
<a name="line798"></a>
<a name="line799"></a>  // Watchdog timeout.
<a name="line800"></a>  responseTimeout();
<a name="line801"></a>  assertNotNull(channel.forwardChannelTimerId_);
<a name="line802"></a>  assertNull(getSingleForwardRequest());
<a name="line803"></a>  assertEquals(1, channel.forwardChannelRetryCount_);
<a name="line804"></a>
<a name="line805"></a>  // Almost finish the between-retry timeout.
<a name="line806"></a>  mockClock.tick(RETRY_TIME - 1);
<a name="line807"></a>  assertNotNull(channel.forwardChannelTimerId_);
<a name="line808"></a>  assertNull(getSingleForwardRequest());
<a name="line809"></a>  assertEquals(1, channel.forwardChannelRetryCount_);
<a name="line810"></a>
<a name="line811"></a>  // Setting max retries to 0 should cancel the timer and raise an error.
<a name="line812"></a>  channel.setFailFast(true);
<a name="line813"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line814"></a>  assertNull(getSingleForwardRequest());
<a name="line815"></a>  assertEquals(1, channel.forwardChannelRetryCount_);
<a name="line816"></a>
<a name="line817"></a>  assertTrue(gotError);
<a name="line818"></a>  assertEquals(0, deliveredMaps.length);
<a name="line819"></a>  // We get the error immediately before starting to ping google.com.
<a name="line820"></a>  // Simulate that timing out. We should get a network error in addition to the
<a name="line821"></a>  // initial failure.
<a name="line822"></a>  gotError = false;
<a name="line823"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line824"></a>  assertTrue(&#39;No error after network ping timed out.&#39;, gotError);
<a name="line825"></a>
<a name="line826"></a>  // Make sure no more retry timers are firing.
<a name="line827"></a>  mockClock.tick(ALL_DAY_MS);
<a name="line828"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line829"></a>  assertNull(getSingleForwardRequest());
<a name="line830"></a>  assertEquals(1, channel.forwardChannelRetryCount_);
<a name="line831"></a>  assertEquals(1, numTimingEvents);
<a name="line832"></a>}
<a name="line833"></a>
<a name="line834"></a>
<a name="line835"></a>/**
<a name="line836"></a> * Make sure that dropping the forward channel retry limit below the retry count
<a name="line837"></a> * reports an error, and prevents another request from firing.
<a name="line838"></a> */
<a name="line839"></a>function testSetFailFastWhileRetryXhrIsInFlight() {
<a name="line840"></a>  stubNetUtils();
<a name="line841"></a>
<a name="line842"></a>  connect();
<a name="line843"></a>  setFailFastWhileRetryXhrIsInFlight();
<a name="line844"></a>}
<a name="line845"></a>
<a name="line846"></a>
<a name="line847"></a>function testSetFailFastWhileRetryXhrIsInFlightWithSpdyEnabled() {
<a name="line848"></a>  stubNetUtils();
<a name="line849"></a>
<a name="line850"></a>  connect(undefined, undefined, undefined, true);
<a name="line851"></a>  setFailFastWhileRetryXhrIsInFlight();
<a name="line852"></a>}
<a name="line853"></a>
<a name="line854"></a>
<a name="line855"></a>function setFailFastWhileRetryXhrIsInFlight() {
<a name="line856"></a>  assertEquals(1, numTimingEvents);
<a name="line857"></a>
<a name="line858"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line859"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line860"></a>  assertNotNull(getSingleForwardRequest());
<a name="line861"></a>  assertEquals(0, channel.forwardChannelRetryCount_);
<a name="line862"></a>
<a name="line863"></a>  // Watchdog timeout.
<a name="line864"></a>  responseTimeout();
<a name="line865"></a>  assertNotNull(channel.forwardChannelTimerId_);
<a name="line866"></a>  assertNull(getSingleForwardRequest());
<a name="line867"></a>  assertEquals(1, channel.forwardChannelRetryCount_);
<a name="line868"></a>
<a name="line869"></a>  // Wait for the between-retry timeout.
<a name="line870"></a>  mockClock.tick(RETRY_TIME);
<a name="line871"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line872"></a>  assertNotNull(getSingleForwardRequest());
<a name="line873"></a>  assertEquals(1, channel.forwardChannelRetryCount_);
<a name="line874"></a>
<a name="line875"></a>  // Simulate a second watchdog timeout.
<a name="line876"></a>  responseTimeout();
<a name="line877"></a>  assertNotNull(channel.forwardChannelTimerId_);
<a name="line878"></a>  assertNull(getSingleForwardRequest());
<a name="line879"></a>  assertEquals(2, channel.forwardChannelRetryCount_);
<a name="line880"></a>
<a name="line881"></a>  // Wait for another between-retry timeout.
<a name="line882"></a>  mockClock.tick(RETRY_TIME);
<a name="line883"></a>  // Now the third req is in flight.
<a name="line884"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line885"></a>  assertNotNull(getSingleForwardRequest());
<a name="line886"></a>  assertEquals(2, channel.forwardChannelRetryCount_);
<a name="line887"></a>
<a name="line888"></a>  // Set fail fast, killing the request
<a name="line889"></a>  channel.setFailFast(true);
<a name="line890"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line891"></a>  assertNull(getSingleForwardRequest());
<a name="line892"></a>  assertEquals(2, channel.forwardChannelRetryCount_);
<a name="line893"></a>
<a name="line894"></a>  assertTrue(gotError);
<a name="line895"></a>  // We get the error immediately before starting to ping google.com.
<a name="line896"></a>  // Simulate that timing out. We should get a network error in addition to the
<a name="line897"></a>  gotError = false;
<a name="line898"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line899"></a>  assertTrue(&#39;No error after network ping timed out.&#39;, gotError);
<a name="line900"></a>
<a name="line901"></a>  // Make sure no more retry timers are firing.
<a name="line902"></a>  mockClock.tick(ALL_DAY_MS);
<a name="line903"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line904"></a>  assertNull(getSingleForwardRequest());
<a name="line905"></a>  assertEquals(2, channel.forwardChannelRetryCount_);
<a name="line906"></a>  assertEquals(1, numTimingEvents);
<a name="line907"></a>}
<a name="line908"></a>
<a name="line909"></a>
<a name="line910"></a>/**
<a name="line911"></a> * Makes sure that setting fail fast while not retrying doesn&#39;t cause a failure.
<a name="line912"></a> */
<a name="line913"></a>function testSetFailFastAtRetryCount() {
<a name="line914"></a>  stubNetUtils();
<a name="line915"></a>
<a name="line916"></a>  connect();
<a name="line917"></a>  assertEquals(1, numTimingEvents);
<a name="line918"></a>
<a name="line919"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line920"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line921"></a>  assertNotNull(getSingleForwardRequest());
<a name="line922"></a>  assertEquals(0, channel.forwardChannelRetryCount_);
<a name="line923"></a>
<a name="line924"></a>  // Set fail fast.
<a name="line925"></a>  channel.setFailFast(true);
<a name="line926"></a>  // Request should still be alive.
<a name="line927"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line928"></a>  assertNotNull(getSingleForwardRequest());
<a name="line929"></a>  assertEquals(0, channel.forwardChannelRetryCount_);
<a name="line930"></a>
<a name="line931"></a>  // Watchdog timeout. Now we should get an error.
<a name="line932"></a>  responseTimeout();
<a name="line933"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line934"></a>  assertNull(getSingleForwardRequest());
<a name="line935"></a>  assertEquals(0, channel.forwardChannelRetryCount_);
<a name="line936"></a>
<a name="line937"></a>  assertTrue(gotError);
<a name="line938"></a>  // We get the error immediately before starting to ping google.com.
<a name="line939"></a>  // Simulate that timing out. We should get a network error in addition to the
<a name="line940"></a>  // initial failure.
<a name="line941"></a>  gotError = false;
<a name="line942"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line943"></a>  assertTrue(&#39;No error after network ping timed out.&#39;, gotError);
<a name="line944"></a>
<a name="line945"></a>  // Make sure no more retry timers are firing.
<a name="line946"></a>  mockClock.tick(ALL_DAY_MS);
<a name="line947"></a>  assertNull(channel.forwardChannelTimerId_);
<a name="line948"></a>  assertNull(getSingleForwardRequest());
<a name="line949"></a>  assertEquals(0, channel.forwardChannelRetryCount_);
<a name="line950"></a>  assertEquals(1, numTimingEvents);
<a name="line951"></a>}
<a name="line952"></a>
<a name="line953"></a>
<a name="line954"></a>function testRequestFailedClosesChannel() {
<a name="line955"></a>  stubNetUtils();
<a name="line956"></a>
<a name="line957"></a>  connect();
<a name="line958"></a>  requestFailedClosesChannel();
<a name="line959"></a>}
<a name="line960"></a>
<a name="line961"></a>
<a name="line962"></a>function testRequestFailedClosesChannelWithSpdyEnabled() {
<a name="line963"></a>  stubNetUtils();
<a name="line964"></a>
<a name="line965"></a>  connect(undefined, undefined, undefined, true);
<a name="line966"></a>  requestFailedClosesChannel();
<a name="line967"></a>}
<a name="line968"></a>
<a name="line969"></a>
<a name="line970"></a>function requestFailedClosesChannel() {
<a name="line971"></a>  assertEquals(1, numTimingEvents);
<a name="line972"></a>
<a name="line973"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line974"></a>  responseRequestFailed();
<a name="line975"></a>
<a name="line976"></a>  assertEquals(&#39;Should be closed immediately after request failed.&#39;,
<a name="line977"></a>      goog.labs.net.webChannel.WebChannelBase.State.CLOSED, channel.getState());
<a name="line978"></a>
<a name="line979"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line980"></a>
<a name="line981"></a>  assertEquals(&#39;Should remain closed after the ping timeout.&#39;,
<a name="line982"></a>      goog.labs.net.webChannel.WebChannelBase.State.CLOSED, channel.getState());
<a name="line983"></a>  assertEquals(1, numTimingEvents);
<a name="line984"></a>}
<a name="line985"></a>
<a name="line986"></a>
<a name="line987"></a>function testStatEventReportedOnlyOnce() {
<a name="line988"></a>  stubNetUtils();
<a name="line989"></a>
<a name="line990"></a>  connect();
<a name="line991"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line992"></a>  numStatEvents = 0;
<a name="line993"></a>  lastStatEvent = null;
<a name="line994"></a>  responseUnknownSessionId();
<a name="line995"></a>
<a name="line996"></a>  assertEquals(1, numStatEvents);
<a name="line997"></a>  assertEquals(goog.labs.net.webChannel.requestStats.Stat.ERROR_OTHER,
<a name="line998"></a>      lastStatEvent);
<a name="line999"></a>
<a name="line1000"></a>  numStatEvents = 0;
<a name="line1001"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line1002"></a>  assertEquals(&#39;No new stat events should be reported.&#39;, 0, numStatEvents);
<a name="line1003"></a>}
<a name="line1004"></a>
<a name="line1005"></a>
<a name="line1006"></a>function testActiveXBlockedEventReportedOnlyOnce() {
<a name="line1007"></a>  stubNetUtils();
<a name="line1008"></a>
<a name="line1009"></a>  connectForwardChannel();
<a name="line1010"></a>  numStatEvents = 0;
<a name="line1011"></a>  lastStatEvent = null;
<a name="line1012"></a>  responseActiveXBlocked();
<a name="line1013"></a>
<a name="line1014"></a>  assertEquals(1, numStatEvents);
<a name="line1015"></a>  assertEquals(goog.labs.net.webChannel.requestStats.Stat.ERROR_OTHER,
<a name="line1016"></a>      lastStatEvent);
<a name="line1017"></a>
<a name="line1018"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line1019"></a>  assertEquals(&#39;No new stat events should be reported.&#39;, 1, numStatEvents);
<a name="line1020"></a>}
<a name="line1021"></a>
<a name="line1022"></a>
<a name="line1023"></a>function testStatEventReportedOnlyOnce_onNetworkUp() {
<a name="line1024"></a>  stubNetUtils();
<a name="line1025"></a>
<a name="line1026"></a>  connect();
<a name="line1027"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line1028"></a>  numStatEvents = 0;
<a name="line1029"></a>  lastStatEvent = null;
<a name="line1030"></a>  responseRequestFailed();
<a name="line1031"></a>
<a name="line1032"></a>  assertEquals(&#39;No stat event should be reported before we know the reason.&#39;,
<a name="line1033"></a>      0, numStatEvents);
<a name="line1034"></a>
<a name="line1035"></a>  // Let the ping time out.
<a name="line1036"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line1037"></a>
<a name="line1038"></a>  // Assert we report the correct stat event.
<a name="line1039"></a>  assertEquals(1, numStatEvents);
<a name="line1040"></a>  assertEquals(
<a name="line1041"></a>      goog.labs.net.webChannel.requestStats.Stat.ERROR_NETWORK,
<a name="line1042"></a>      lastStatEvent);
<a name="line1043"></a>}
<a name="line1044"></a>
<a name="line1045"></a>
<a name="line1046"></a>function testStatEventReportedOnlyOnce_onNetworkDown() {
<a name="line1047"></a>  stubNetUtils();
<a name="line1048"></a>
<a name="line1049"></a>  connect();
<a name="line1050"></a>  sendMap(&#39;foo&#39;, &#39;bar&#39;);
<a name="line1051"></a>  numStatEvents = 0;
<a name="line1052"></a>  lastStatEvent = null;
<a name="line1053"></a>  responseRequestFailed();
<a name="line1054"></a>
<a name="line1055"></a>  assertEquals(&#39;No stat event should be reported before we know the reason.&#39;,
<a name="line1056"></a>      0, numStatEvents);
<a name="line1057"></a>
<a name="line1058"></a>  // Wait half the ping timeout period, and then fake the network being up.
<a name="line1059"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT / 2);
<a name="line1060"></a>  channel.testNetworkCallback_(true);
<a name="line1061"></a>
<a name="line1062"></a>  // Assert we report the correct stat event.
<a name="line1063"></a>  assertEquals(1, numStatEvents);
<a name="line1064"></a>  assertEquals(goog.labs.net.webChannel.requestStats.Stat.ERROR_OTHER,
<a name="line1065"></a>      lastStatEvent);
<a name="line1066"></a>}
<a name="line1067"></a>
<a name="line1068"></a>
<a name="line1069"></a>function testOutgoingMapsAwaitsResponse() {
<a name="line1070"></a>  connect();
<a name="line1071"></a>  outgoingMapsAwaitsResponse();
<a name="line1072"></a>}
<a name="line1073"></a>
<a name="line1074"></a>
<a name="line1075"></a>function testOutgoingMapsAwaitsResponseWithSpdyEnabled() {
<a name="line1076"></a>  connect(undefined, undefined, undefined, true);
<a name="line1077"></a>  outgoingMapsAwaitsResponse();
<a name="line1078"></a>}
<a name="line1079"></a>
<a name="line1080"></a>
<a name="line1081"></a>function outgoingMapsAwaitsResponse() {
<a name="line1082"></a>  assertEquals(0, channel.outgoingMaps_.length);
<a name="line1083"></a>
<a name="line1084"></a>  sendMap(&#39;foo1&#39;, &#39;bar&#39;);
<a name="line1085"></a>  assertEquals(0, channel.outgoingMaps_.length);
<a name="line1086"></a>  sendMap(&#39;foo2&#39;, &#39;bar&#39;);
<a name="line1087"></a>  assertEquals(1, channel.outgoingMaps_.length);
<a name="line1088"></a>  sendMap(&#39;foo3&#39;, &#39;bar&#39;);
<a name="line1089"></a>  assertEquals(2, channel.outgoingMaps_.length);
<a name="line1090"></a>  sendMap(&#39;foo4&#39;, &#39;bar&#39;);
<a name="line1091"></a>  assertEquals(3, channel.outgoingMaps_.length);
<a name="line1092"></a>
<a name="line1093"></a>  responseDone();
<a name="line1094"></a>  // Now the forward channel request is completed and a new started, so all maps
<a name="line1095"></a>  // are dequeued from the array of outgoing maps into this new forward request.
<a name="line1096"></a>  assertEquals(0, channel.outgoingMaps_.length);
<a name="line1097"></a>}
<a name="line1098"></a>
<a name="line1099"></a>
<a name="line1100"></a>function testUndeliveredMaps_doesNotNotifyWhenSuccessful() {
<a name="line1101"></a>  /**
<a name="line1102"></a>   * @suppress {checkTypes} The callback function type declaration is skipped.
<a name="line1103"></a>   */
<a name="line1104"></a>  handler.channelClosed = function(
<a name="line1105"></a>      channel, opt_pendingMaps, opt_undeliveredMaps) {
<a name="line1106"></a>    if (opt_pendingMaps || opt_undeliveredMaps) {
<a name="line1107"></a>      fail(&#39;No pending or undelivered maps should be reported.&#39;);
<a name="line1108"></a>    }
<a name="line1109"></a>  };
<a name="line1110"></a>
<a name="line1111"></a>  connect();
<a name="line1112"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1113"></a>  responseDone();
<a name="line1114"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line1115"></a>  responseDone();
<a name="line1116"></a>  disconnect();
<a name="line1117"></a>}
<a name="line1118"></a>
<a name="line1119"></a>
<a name="line1120"></a>function testUndeliveredMaps_doesNotNotifyIfNothingWasSent() {
<a name="line1121"></a>  /**
<a name="line1122"></a>   * @suppress {checkTypes} The callback function type declaration is skipped.
<a name="line1123"></a>   */
<a name="line1124"></a>  handler.channelClosed = function(
<a name="line1125"></a>      channel, opt_pendingMaps, opt_undeliveredMaps) {
<a name="line1126"></a>    if (opt_pendingMaps || opt_undeliveredMaps) {
<a name="line1127"></a>      fail(&#39;No pending or undelivered maps should be reported.&#39;);
<a name="line1128"></a>    }
<a name="line1129"></a>  };
<a name="line1130"></a>
<a name="line1131"></a>  connect();
<a name="line1132"></a>  mockClock.tick(ALL_DAY_MS);
<a name="line1133"></a>  disconnect();
<a name="line1134"></a>}
<a name="line1135"></a>
<a name="line1136"></a>
<a name="line1137"></a>function testUndeliveredMaps_clearsPendingMapsAfterNotifying() {
<a name="line1138"></a>  connect();
<a name="line1139"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1140"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line1141"></a>  sendMap(&#39;foo3&#39;, &#39;bar3&#39;);
<a name="line1142"></a>
<a name="line1143"></a>  assertEquals(1, channel.pendingMaps_.length);
<a name="line1144"></a>  assertEquals(2, channel.outgoingMaps_.length);
<a name="line1145"></a>
<a name="line1146"></a>  disconnect();
<a name="line1147"></a>
<a name="line1148"></a>  assertEquals(0, channel.pendingMaps_.length);
<a name="line1149"></a>  assertEquals(0, channel.outgoingMaps_.length);
<a name="line1150"></a>}
<a name="line1151"></a>
<a name="line1152"></a>
<a name="line1153"></a>function testUndeliveredMaps_notifiesWithContext() {
<a name="line1154"></a>  connect();
<a name="line1155"></a>
<a name="line1156"></a>  // First send two messages that succeed.
<a name="line1157"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;, &#39;context1&#39;);
<a name="line1158"></a>  responseDone();
<a name="line1159"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;, &#39;context2&#39;);
<a name="line1160"></a>  responseDone();
<a name="line1161"></a>
<a name="line1162"></a>  // Pretend the server hangs and no longer responds.
<a name="line1163"></a>  sendMap(&#39;foo3&#39;, &#39;bar3&#39;, &#39;context3&#39;);
<a name="line1164"></a>  sendMap(&#39;foo4&#39;, &#39;bar4&#39;, &#39;context4&#39;);
<a name="line1165"></a>  sendMap(&#39;foo5&#39;, &#39;bar5&#39;, &#39;context5&#39;);
<a name="line1166"></a>
<a name="line1167"></a>  // Give up.
<a name="line1168"></a>  disconnect();
<a name="line1169"></a>
<a name="line1170"></a>  // Assert that we are informed of any undelivered messages; both about
<a name="line1171"></a>  // #3 that was sent but which we don&#39;t know if the server received, and
<a name="line1172"></a>  // #4 and #5 which remain in the outgoing maps and have not yet been sent.
<a name="line1173"></a>  assertEquals(&#39;foo3:bar3:context3&#39;, handler.pendingMapsString);
<a name="line1174"></a>  assertEquals(&#39;foo4:bar4:context4, foo5:bar5:context5&#39;,
<a name="line1175"></a>      handler.undeliveredMapsString);
<a name="line1176"></a>}
<a name="line1177"></a>
<a name="line1178"></a>
<a name="line1179"></a>function testUndeliveredMaps_serviceUnavailable() {
<a name="line1180"></a>  // Send a few maps, and let one fail.
<a name="line1181"></a>  connect();
<a name="line1182"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1183"></a>  responseDone();
<a name="line1184"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line1185"></a>  responseRequestFailed();
<a name="line1186"></a>
<a name="line1187"></a>  // After a failure, the channel should be closed.
<a name="line1188"></a>  disconnect();
<a name="line1189"></a>
<a name="line1190"></a>  assertEquals(&#39;foo2:bar2&#39;, handler.pendingMapsString);
<a name="line1191"></a>  assertEquals(&#39;&#39;, handler.undeliveredMapsString);
<a name="line1192"></a>}
<a name="line1193"></a>
<a name="line1194"></a>
<a name="line1195"></a>function testUndeliveredMaps_onPingTimeout() {
<a name="line1196"></a>  stubNetUtils();
<a name="line1197"></a>
<a name="line1198"></a>  connect();
<a name="line1199"></a>
<a name="line1200"></a>  // Send a message.
<a name="line1201"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1202"></a>
<a name="line1203"></a>  // Fake REQUEST_FAILED, triggering a ping to check the network.
<a name="line1204"></a>  responseRequestFailed();
<a name="line1205"></a>
<a name="line1206"></a>  // Let the ping time out, unsuccessfully.
<a name="line1207"></a>  mockClock.tick(goog.labs.net.webChannel.netUtils.NETWORK_TIMEOUT);
<a name="line1208"></a>
<a name="line1209"></a>  // Assert channel is closed.
<a name="line1210"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.CLOSED,
<a name="line1211"></a>      channel.getState());
<a name="line1212"></a>
<a name="line1213"></a>  // Assert that the handler is notified about the undelivered messages.
<a name="line1214"></a>  assertEquals(&#39;foo1:bar1&#39;, handler.pendingMapsString);
<a name="line1215"></a>  assertEquals(&#39;&#39;, handler.undeliveredMapsString);
<a name="line1216"></a>}
<a name="line1217"></a>
<a name="line1218"></a>
<a name="line1219"></a>function testResponseNoBackchannelPostNotBeforeBackchannel() {
<a name="line1220"></a>  connect(8);
<a name="line1221"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1222"></a>
<a name="line1223"></a>  mockClock.tick(10);
<a name="line1224"></a>  assertFalse(channel.backChannelRequest_.getRequestStartTime() &lt;
<a name="line1225"></a>      getSingleForwardRequest().getRequestStartTime());
<a name="line1226"></a>  responseNoBackchannel();
<a name="line1227"></a>  assertNotEquals(
<a name="line1228"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_MISSING,
<a name="line1229"></a>      lastStatEvent);
<a name="line1230"></a>}
<a name="line1231"></a>
<a name="line1232"></a>
<a name="line1233"></a>function testResponseNoBackchannel() {
<a name="line1234"></a>  connect(8);
<a name="line1235"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1236"></a>  response(-1, 0);
<a name="line1237"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE + 1);
<a name="line1238"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line1239"></a>  assertTrue(channel.backChannelRequest_.getRequestStartTime() +
<a name="line1240"></a>      goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE &lt;
<a name="line1241"></a>      getSingleForwardRequest().getRequestStartTime());
<a name="line1242"></a>  responseNoBackchannel();
<a name="line1243"></a>  assertEquals(
<a name="line1244"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_MISSING,
<a name="line1245"></a>      lastStatEvent);
<a name="line1246"></a>}
<a name="line1247"></a>
<a name="line1248"></a>
<a name="line1249"></a>function testResponseNoBackchannelWithNoBackchannel() {
<a name="line1250"></a>  connect(8);
<a name="line1251"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1252"></a>  assertNull(channel.backChannelTimerId_);
<a name="line1253"></a>  channel.backChannelRequest_.cancel();
<a name="line1254"></a>  channel.backChannelRequest_ = null;
<a name="line1255"></a>  responseNoBackchannel();
<a name="line1256"></a>  assertEquals(
<a name="line1257"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_MISSING,
<a name="line1258"></a>      lastStatEvent);
<a name="line1259"></a>}
<a name="line1260"></a>
<a name="line1261"></a>
<a name="line1262"></a>function testResponseNoBackchannelWithStartTimer() {
<a name="line1263"></a>  connect(8);
<a name="line1264"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1265"></a>
<a name="line1266"></a>  channel.backChannelRequest_.cancel();
<a name="line1267"></a>  channel.backChannelRequest_ = null;
<a name="line1268"></a>  channel.backChannelTimerId_ = 123;
<a name="line1269"></a>  responseNoBackchannel();
<a name="line1270"></a>  assertNotEquals(
<a name="line1271"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_MISSING,
<a name="line1272"></a>      lastStatEvent);
<a name="line1273"></a>}
<a name="line1274"></a>
<a name="line1275"></a>
<a name="line1276"></a>function testResponseWithNoArraySent() {
<a name="line1277"></a>  connect(8);
<a name="line1278"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1279"></a>
<a name="line1280"></a>  // Send a response as if the server hasn&#39;t sent down an array.
<a name="line1281"></a>  response(-1, 0);
<a name="line1282"></a>
<a name="line1283"></a>  // POST response with an array ID lower than our last received is OK.
<a name="line1284"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1285"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1286"></a>}
<a name="line1287"></a>
<a name="line1288"></a>
<a name="line1289"></a>function testResponseWithArraysMissing() {
<a name="line1290"></a>  connect(8);
<a name="line1291"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1292"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1293"></a>
<a name="line1294"></a>  // Send a response as if the server has sent down seven arrays.
<a name="line1295"></a>  response(7, 111);
<a name="line1296"></a>
<a name="line1297"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1298"></a>  assertEquals(7, channel.lastPostResponseArrayId_);
<a name="line1299"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE * 2);
<a name="line1300"></a>  assertEquals(
<a name="line1301"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_DEAD,
<a name="line1302"></a>      lastStatEvent);
<a name="line1303"></a>}
<a name="line1304"></a>
<a name="line1305"></a>
<a name="line1306"></a>function testMultipleResponsesWithArraysMissing() {
<a name="line1307"></a>  connect(8);
<a name="line1308"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1309"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1310"></a>
<a name="line1311"></a>  // Send a response as if the server has sent down seven arrays.
<a name="line1312"></a>  response(7, 111);
<a name="line1313"></a>
<a name="line1314"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1315"></a>  assertEquals(7, channel.lastPostResponseArrayId_);
<a name="line1316"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line1317"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE);
<a name="line1318"></a>  response(8, 119);
<a name="line1319"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE);
<a name="line1320"></a>  // The original timer should still fire.
<a name="line1321"></a>  assertEquals(
<a name="line1322"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_DEAD,
<a name="line1323"></a>      lastStatEvent);
<a name="line1324"></a>}
<a name="line1325"></a>
<a name="line1326"></a>
<a name="line1327"></a>function testOnlyRetryOnceBasedOnResponse() {
<a name="line1328"></a>  connect(8);
<a name="line1329"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1330"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1331"></a>
<a name="line1332"></a>  // Send a response as if the server has sent down seven arrays.
<a name="line1333"></a>  response(7, 111);
<a name="line1334"></a>
<a name="line1335"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1336"></a>  assertEquals(7, channel.lastPostResponseArrayId_);
<a name="line1337"></a>  assertTrue(hasDeadBackChannelTimer());
<a name="line1338"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE * 2);
<a name="line1339"></a>  assertEquals(
<a name="line1340"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_DEAD,
<a name="line1341"></a>      lastStatEvent);
<a name="line1342"></a>  assertEquals(1, channel.backChannelRetryCount_);
<a name="line1343"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE);
<a name="line1344"></a>  sendMap(&#39;foo2&#39;, &#39;bar2&#39;);
<a name="line1345"></a>  assertFalse(hasDeadBackChannelTimer());
<a name="line1346"></a>  response(8, 119);
<a name="line1347"></a>  assertFalse(hasDeadBackChannelTimer());
<a name="line1348"></a>}
<a name="line1349"></a>
<a name="line1350"></a>
<a name="line1351"></a>function testResponseWithArraysMissingAndLiveChannel() {
<a name="line1352"></a>  connect(8);
<a name="line1353"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1354"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1355"></a>
<a name="line1356"></a>  // Send a response as if the server has sent down seven arrays.
<a name="line1357"></a>  response(7, 111);
<a name="line1358"></a>
<a name="line1359"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1360"></a>  assertEquals(7, channel.lastPostResponseArrayId_);
<a name="line1361"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE);
<a name="line1362"></a>  assertTrue(hasDeadBackChannelTimer());
<a name="line1363"></a>  receive(&#39;[&quot;ack&quot;]&#39;);
<a name="line1364"></a>  assertFalse(hasDeadBackChannelTimer());
<a name="line1365"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE);
<a name="line1366"></a>  assertNotEquals(
<a name="line1367"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_DEAD,
<a name="line1368"></a>      lastStatEvent);
<a name="line1369"></a>}
<a name="line1370"></a>
<a name="line1371"></a>
<a name="line1372"></a>function testResponseWithBigOutstandingData() {
<a name="line1373"></a>  connect(8);
<a name="line1374"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1375"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1376"></a>
<a name="line1377"></a>  // Send a response as if the server has sent down seven arrays and 50kbytes.
<a name="line1378"></a>  response(7, 50000);
<a name="line1379"></a>
<a name="line1380"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1381"></a>  assertEquals(7, channel.lastPostResponseArrayId_);
<a name="line1382"></a>  assertFalse(hasDeadBackChannelTimer());
<a name="line1383"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE * 2);
<a name="line1384"></a>  assertNotEquals(
<a name="line1385"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_DEAD,
<a name="line1386"></a>      lastStatEvent);
<a name="line1387"></a>}
<a name="line1388"></a>
<a name="line1389"></a>
<a name="line1390"></a>function testResponseInBufferedMode() {
<a name="line1391"></a>  connect(8);
<a name="line1392"></a>  channel.useChunked_ = false;
<a name="line1393"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1394"></a>  assertEquals(-1, channel.lastPostResponseArrayId_);
<a name="line1395"></a>  response(7, 111);
<a name="line1396"></a>
<a name="line1397"></a>  assertEquals(1, channel.lastArrayId_);
<a name="line1398"></a>  assertEquals(7, channel.lastPostResponseArrayId_);
<a name="line1399"></a>  assertFalse(hasDeadBackChannelTimer());
<a name="line1400"></a>  mockClock.tick(goog.labs.net.webChannel.WebChannelBase.RTT_ESTIMATE * 2);
<a name="line1401"></a>  assertNotEquals(
<a name="line1402"></a>      goog.labs.net.webChannel.requestStats.Stat.BACKCHANNEL_DEAD,
<a name="line1403"></a>      lastStatEvent);
<a name="line1404"></a>}
<a name="line1405"></a>
<a name="line1406"></a>
<a name="line1407"></a>function testResponseWithGarbage() {
<a name="line1408"></a>  connect(8);
<a name="line1409"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1410"></a>  channel.onRequestData(
<a name="line1411"></a>      getSingleForwardRequest(),
<a name="line1412"></a>      &#39;garbage&#39;
<a name="line1413"></a>  );
<a name="line1414"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.CLOSED,
<a name="line1415"></a>      channel.getState());
<a name="line1416"></a>}
<a name="line1417"></a>
<a name="line1418"></a>
<a name="line1419"></a>function testResponseWithGarbageInArray() {
<a name="line1420"></a>  connect(8);
<a name="line1421"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1422"></a>  channel.onRequestData(
<a name="line1423"></a>      getSingleForwardRequest(),
<a name="line1424"></a>      &#39;[&quot;garbage&quot;]&#39;
<a name="line1425"></a>  );
<a name="line1426"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.CLOSED,
<a name="line1427"></a>      channel.getState());
<a name="line1428"></a>}
<a name="line1429"></a>
<a name="line1430"></a>
<a name="line1431"></a>function testResponseWithEvilData() {
<a name="line1432"></a>  connect(8);
<a name="line1433"></a>  sendMap(&#39;foo1&#39;, &#39;bar1&#39;);
<a name="line1434"></a>  channel.onRequestData(
<a name="line1435"></a>      getSingleForwardRequest(),
<a name="line1436"></a>      &#39;foo=&lt;script&gt;evil()\&lt;\/script&gt;&amp;&#39; + &#39;bar=&lt;script&gt;moreEvil()\&lt;\/script&gt;&#39;);
<a name="line1437"></a>  assertEquals(goog.labs.net.webChannel.WebChannelBase.State.CLOSED,
<a name="line1438"></a>      channel.getState());
<a name="line1439"></a>}
<a name="line1440"></a>
<a name="line1441"></a>
<a name="line1442"></a>function testPathAbsolute() {
<a name="line1443"></a>  connect(8, undefined, &#39;/talkgadget&#39;);
<a name="line1444"></a>  assertEquals(channel.backChannelUri_.getDomain(),
<a name="line1445"></a>      window.location.hostname);
<a name="line1446"></a>  assertEquals(channel.forwardChannelUri_.getDomain(),
<a name="line1447"></a>      window.location.hostname);
<a name="line1448"></a>}
<a name="line1449"></a>
<a name="line1450"></a>
<a name="line1451"></a>function testPathRelative() {
<a name="line1452"></a>  connect(8, undefined, &#39;talkgadget&#39;);
<a name="line1453"></a>  assertEquals(channel.backChannelUri_.getDomain(),
<a name="line1454"></a>      window.location.hostname);
<a name="line1455"></a>  assertEquals(channel.forwardChannelUri_.getDomain(),
<a name="line1456"></a>      window.location.hostname);
<a name="line1457"></a>}
<a name="line1458"></a>
<a name="line1459"></a>
<a name="line1460"></a>function testPathWithHost() {
<a name="line1461"></a>  connect(8, undefined, &#39;https://example.com&#39;);
<a name="line1462"></a>  assertEquals(channel.backChannelUri_.getScheme(), &#39;https&#39;);
<a name="line1463"></a>  assertEquals(channel.backChannelUri_.getDomain(), &#39;example.com&#39;);
<a name="line1464"></a>  assertEquals(channel.forwardChannelUri_.getScheme(), &#39;https&#39;);
<a name="line1465"></a>  assertEquals(channel.forwardChannelUri_.getDomain(), &#39;example.com&#39;);
<a name="line1466"></a>}
<a name="line1467"></a>
<a name="line1468"></a>function testCreateXhrIo() {
<a name="line1469"></a>  var xhr = channel.createXhrIo(null);
<a name="line1470"></a>  assertFalse(xhr.getWithCredentials());
<a name="line1471"></a>
<a name="line1472"></a>  assertThrows(
<a name="line1473"></a>      &#39;Error connection to different host without CORS&#39;,
<a name="line1474"></a>      goog.bind(channel.createXhrIo, channel, &#39;some_host&#39;));
<a name="line1475"></a>
<a name="line1476"></a>  channel.setSupportsCrossDomainXhrs(true);
<a name="line1477"></a>
<a name="line1478"></a>  xhr = channel.createXhrIo(null);
<a name="line1479"></a>  assertTrue(xhr.getWithCredentials());
<a name="line1480"></a>
<a name="line1481"></a>  xhr = channel.createXhrIo(&#39;some_host&#39;);
<a name="line1482"></a>  assertTrue(xhr.getWithCredentials());
<a name="line1483"></a>}
<a name="line1484"></a>
<a name="line1485"></a>function testSpdyLimitOption() {
<a name="line1486"></a>  var webChannelTransport =
<a name="line1487"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line1488"></a>  stubSpdyCheck(true);
<a name="line1489"></a>  var webChannelDefault = webChannelTransport.createWebChannel(&#39;/foo&#39;);
<a name="line1490"></a>  assertEquals(10,
<a name="line1491"></a>      webChannelDefault.getRuntimeProperties().getSpdyRequestLimit());
<a name="line1492"></a>
<a name="line1493"></a>  var options = {&#39;spdyRequestLimit&#39;: 100};
<a name="line1494"></a>
<a name="line1495"></a>  stubSpdyCheck(false);
<a name="line1496"></a>  var webChannelDisabled = webChannelTransport.createWebChannel(
<a name="line1497"></a>      &#39;/foo&#39;, options);
<a name="line1498"></a>  assertEquals(1,
<a name="line1499"></a>      webChannelDisabled.getRuntimeProperties().getSpdyRequestLimit());
<a name="line1500"></a>
<a name="line1501"></a>  stubSpdyCheck(true);
<a name="line1502"></a>  var webChannelEnabled = webChannelTransport.createWebChannel(&#39;/foo&#39;, options);
<a name="line1503"></a>  assertEquals(100,
<a name="line1504"></a>      webChannelEnabled.getRuntimeProperties().getSpdyRequestLimit());
<a name="line1505"></a>}
</pre>


</body>
</html>
